﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Maxel.MultitrackControl
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			for (int i = 0; i < 100; i++)
			{
				Panel panel = new Panel();
				panel.BackColor = Color.White;
				panel.Name = "Panel"+i;
				panel.MouseEnter  += delegate(object sender1, EventArgs e1) {
					toolTip1.Show(((Panel)sender1).Name, this, this.PointToClient(MousePosition), 2000);
				}; 
				flowLayoutPanel1.Controls.Add(panel); 
			}
		}

		private void ShowInfo()
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("Bounds:[{0},{1}]", flowLayoutPanel1.Bounds.Height, flowLayoutPanel1.Bounds.Width);
			sb.AppendFormat("AutoScrollPosition:[{0},{1}]", flowLayoutPanel1.AutoScrollPosition.X, flowLayoutPanel1.AutoScrollPosition.Y);
			sb.AppendFormat("ClientRectangle:[{0},{1}]", flowLayoutPanel1.ClientRectangle.Height, flowLayoutPanel1.ClientRectangle.Width);
			sb.AppendFormat("ClientSize:[{0},{1}]", flowLayoutPanel1.ClientSize.Height, flowLayoutPanel1.ClientSize.Width);
			sb.AppendFormat("DisplayRectangle:[{0},{1}]", flowLayoutPanel1.DisplayRectangle.Height, flowLayoutPanel1.DisplayRectangle.Width);
			sb.AppendFormat("Scale:{0}->{1} [{2}]",previousScale , scale ,calcFactor );


			label1.Text = sb.ToString();
			
			flowLayoutPanel1.HorizontalScroll.Visible = false;
  
		}

		private void button1_Click(object sender, EventArgs e)
		{
			Scroll(-1);
		}

		private void button2_Click(object sender, EventArgs e)
		{
			Scroll(1);
		}

		private void button3_Click(object sender, EventArgs e)
		{
			Scroll(-DisplayRectangle.Width);
		}

		private void button4_Click(object sender, EventArgs e)
		{
			Scroll(DisplayRectangle.Width);
		}

		private void flowLayoutPanel1_Scroll(object sender, ScrollEventArgs e)
		{
			ShowInfo();
		}

		double scale = 100;
		double previousScale = 100;
		double delta = 1;

		private void button5_Click(object sender, EventArgs e)
		{
			if (scale - delta < 10)
				return;
			previousScale = scale;
			scale -= delta;
			if (scale == 0)
				return;
			//flowLayoutPanel1.Scale(new SizeF(scale, 1f));
			Scale();
		}

		private void button6_Click(object sender, EventArgs e)
		{
			//if (scale <= 20)
			//	return;
			previousScale = scale;
			scale += delta;
			//flowLayoutPanel1.Scale(new SizeF(scale, 1f));
			Scale();
		}

		private void Scroll(int offset)
		{
			Point p = flowLayoutPanel1.AutoScrollPosition;
			flowLayoutPanel1.AutoScrollPosition = new Point(Math.Abs(p.X) + offset, p.Y);
			ShowInfo();

		}
		float calcFactor = 0;
		private void Scale()
		{
			
			int x = scale - previousScale > 0 ? 1 : -1;
			//calcFactor=(float)(scale / previousScale);
			calcFactor = 1f + (float)(x * delta / previousScale);
			flowLayoutPanel1.SuspendLayout(); 
			foreach (Control item in flowLayoutPanel1.Controls)
			{
				item.Scale(new SizeF(calcFactor, 1f));
			}
			flowLayoutPanel1.ResumeLayout(); 
			ShowInfo();
		}

		[DllImport("user32.dll")]
		static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);

		private const int SB_HORZ=0;

		private void flowLayoutPanel1_SizeChanged(object sender, EventArgs e)
		{
			ShowScrollBar(((Control)sender).Handle, SB_HORZ, false);
		}

	}

}

